package ast;

import java.util.ArrayList;

import symbol.astPreprocessContext;

public class Program extends AstItem {
	// 程序父节点
	// 全局变量声明 LValStatement类
	public ArrayList<AstItem> globalVarDecl = new ArrayList<AstItem>();
	// ClassDef类
	public ArrayList<AstItem> classesDefinition = new ArrayList<AstItem>();
	// FuncDef类
	public ArrayList<AstItem> functionsDefinition = new ArrayList<AstItem>();
	// MainFunc类
	public AstItem mainFunction;
	
	public void preprocess(AstItem father) {
		for (AstItem x : globalVarDecl) {
			x.preprocess(this);
		}
		for (AstItem x : classesDefinition) {
			x.preprocess(this);
		}
		for (AstItem x : functionsDefinition) {
			x.preprocess(this);
		}
		mainFunction.preprocess(this);
	}

	public String toString() {
		System.out.println(astPreprocessContext.toDebugString());
		String resultString = "#include <stdio.h>\n\n";
		for (AstItem x : globalVarDecl) {
			resultString += x.toString();
		}
		resultString += "\n";
		for (AstItem x : classesDefinition) {
			resultString += x.toString();
		}
		resultString += "\n";
		for (AstItem x : functionsDefinition) {
			resultString += x.toString();
		}
		resultString += "\n";
		resultString += mainFunction.toString();
		return resultString;
	}
	
	@Override
	public String toJsonString() {
		String json = "{";
		json += "\"Type\": \"Program\",";
		json += "\"GlobalVarDecls\": [";
		for (AstItem astItem: globalVarDecl) {
			json += astItem.toJsonString();
			json += ",";
		}
		json += "],";
		json += "\"ClassDefinitions\": [";
		for (AstItem astItem: classesDefinition) {
			json += astItem.toJsonString();
			json += ",";
		}
		json += "],";
		json += "\"FunctionDefinitions\": [";
		for (AstItem astItem: functionsDefinition) {
			json += (astItem != null ? astItem.toJsonString() : "\"\"");
			json += ",";
		}
		json += "],";
		json += "\"MainFunction\": ";
		json += mainFunction.toJsonString();
		json += "}";
		return json;
	}
}
